<?php
require_once ("../utils/ConnectionFactory.php");
require_once ("../persistance/data/PromotionDO.php");
require_once ("../persistance/data/HistoriqueDO.php");

/**
 * CRUD pour les promos
 */
class PromotionDAO {
	
	/**
	 * donne la liste des promos
	 */
	public function getListPromoDO($typeArticle) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( 'SELECT * FROM promotion where type_promo = "'.$typeArticle.'"' );
		
		if ($reponse == null) {
			return null;
		}
		
		$listPromotionDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$promotionDO = $this->setPromotionDO ( $data );
			$listPromotionDO->append ( $promotionDO );
		}
		return $listPromotionDO;
	}
	
	/**
	 * Donne l'historique des promos recu pour chaque client
	 */
	public function getHistorique() {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( 'SELECT promo_client.num_promo, nom_client, libelle from promo_client join client on client.num_client = promo_client.num_client join promotion on promotion.num_promo = promo_client.num_promo' );
	
		if ($reponse == null) {
			return null;
		}
	
		$listPromoClientDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$promoClientDO = $this->setHistoriqueDO ( $data );
			$listPromoClientDO->append ( $promoClientDO );
		}
		return $listPromoClientDO;
	}
	
	public function getPromoNonLuClient($numClient) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( 'SELECT promo_client.num_promo, nom_client, libelle from promo_client join client on client.num_client = promo_client.num_client join promotion on promotion.num_promo = promo_client.num_promo where promo_client.num_client = '.$numClient.' and promo_client.lu = 0' );
	
		if ($reponse == null) {
			return null;
		}
	
		$listPromoClientDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$promoClientDO = $this->setHistoriqueDO ( $data );
			$listPromoClientDO->append ( $promoClientDO );
		}
		return $listPromoClientDO;
	}
	
	public function getListPromoDuMomentDO() {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT * FROM promotion WHERE promo_du_moment = 1" );
	
		if ($reponse == null) {
			return null;
		}
	
		$listPromotionDuMomentDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$promotionDO = $this->setPromotionDO ( $data );
			$listPromotionDuMomentDO->append ( $promotionDO );
		}
		return $listPromotionDuMomentDO;
	}
	
	/**
	 * donne la liste des promos
	 */
	public function getListArticlePromoDuMomentDO() {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ( "SELECT * FROM `promotion` join article on promotion.num_article = article.num_article where promo_du_moment = 1" );
		
		if ($reponse == null) {
			return null;
		}
		
		$listPromotionDuMomentDO = new ArrayObject ();
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$promotionDO = $this->setPromotionDuMomentDO ( $data );
			$listPromotionDuMomentDO->append ( $promotionDO );
		}
		return $listPromotionDuMomentDO;
	}
	
	/**
	 * Passe une promo a un promo du moment
	 *
	 * @param PromotionDO $promotionDO        	
	 */
	public function addPromoDuMoment($numeroPromo) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ("UPDATE `ibeacon`.`promotion` SET `promo_du_moment` = '1' WHERE `promotion`.`num_promo` =" . $numeroPromo);
	}

	/**
	 * Passe une promo a un promo du moment
	 *
	 * @param PromotionDO $promotionDO
	 */
	public function removePromoDuMoment($numeroPromo) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ("UPDATE `ibeacon`.`promotion` SET `promo_du_moment` = '0' WHERE `promotion`.`num_promo` =" . $numeroPromo);
	}
	
	
	/**
	 * retourne une promotionDO en fonction de l'id de l'article
	 */
	public function getPromoDO($numeroArticle) {
		$connection = ConnectionFactory::getConnection ();
		$reponse = mysql_query ("SELECT * FROM `promotion` join article on promotion.num_article = article.num_article where promotion.num_article =" . $numeroArticle );
		
		if ($reponse == null) {
			return null;
		}
		
		while ( $data = mysql_fetch_assoc ( $reponse ) ) {
			$promotionDO = $this->setPromotionDuMomentDO ( $data );
		}
		return $promotionDO;
	}
	
	private function setHistoriqueDO($donnee) {
		$historiqueDO = new HistoriqueDO ();
		$historiqueDO->setNumPromo ( $donnee ['num_promo'] );
		$historiqueDO->setNomClient ( $donnee ['nom_client'] );
		$historiqueDO->setLibelle ( $donnee ['libelle'] );
		return $historiqueDO;
	}
	private function setPromotionDO($donnee) {
		$promotionDO = new PromotionDO ();
		$promotionDO->setNumPromo ( $donnee ['num_promo'] );
		$promotionDO->setLibelle ( $donnee ['libelle'] );
		$promotionDO->setPourcentagePromo ( $donnee ['pourcentage_promo'] );
		$promotionDO->setNumArticle ( $donnee ['num_article'] );
		return $promotionDO;
	}
	private function setPromotionDuMomentDO($donnee) {
		$promotionDO = new PromotionDO ();
		$promotionDO->setNumPromo ( $donnee ['num_promo'] );
		$promotionDO->setLibelle ( $donnee ['libelle'] );
		$promotionDO->setPourcentagePromo ( $donnee ['pourcentage_promo'] );
		$promotionDO->setPromoDuMoment ( $donnee ['promo_du_moment'] );
		$promotionDO->setNumArticle ( $donnee ['num_article'] );
		$promotionDO->setLibelleArt ( $donnee ['libelle'] );
		$promotionDO->setPrixUnitaire ( $donnee ['pu'] );
		$promotionDO->setLibelleTypeArticle ( $donnee ['libelle_type_art'] );
		return $promotionDO;
	}
}	




